/*
 *      network_list.h
 *
 *      Copyright 2009 AzureSky <AzureSky.fedora@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#ifndef NETWORK_LIST_H_
#define NETWORK_LIST_H_
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include <cstring>
#include <list>
#include <vector>

class network_list
{
public:
	network_list();
	~network_list();
	
	void read();
	void refresh();
	void start();
	bool next();
	bool is_end();
	std::string& get_ip_protocol();
	std::string& get_local_ip();
	int get_local_port();
	std::string& get_remote_ip();
	int get_remote_port();
	std::string& get_ip_status();

private:
	const std::string m_ipv4_tcp_path;
	const std::string m_ipv4_udp_path;
	const std::string m_ipv6_tcp_path;
	const std::string m_ipv6_udp_path;

	enum ip_protocol{
		TCP = 0,
		UDP
	};

	typedef struct _net_link{
		network_list::ip_protocol protocol;
		std::string local_ip;
		int local_port;
		std::string remote_ip;
		int remote_port;
		int status;
	}net_link;

	typedef net_link* net_link_ptr;
	typedef std::list<net_link_ptr> NETWORK_LIST;
	NETWORK_LIST m_network_list;
	NETWORK_LIST::iterator m_list_it;

	std::vector<std::string> STATUS;
	std::vector<std::string> PROTOCOL;

	void clear_all();
	void read_file(const std::string& path,network_list::ip_protocol protocol);
	void add_link(const std::string& link_string,network_list::ip_protocol protocol);
	int to_ip4(std::string& ip_address,char ip[]);
	void init_data();
};

typedef network_list* network_list_ptr;

#endif /* NETWORK_LIST_H_ */
